from django.http import HttpResponseRedirect
from django.core.paginator import ObjectPaginator
from google.appengine.api import users
from google.appengine.ext.db import Query
from plzkeepthat.common import constants as app, messages, util
from plzkeepthat.models.models import Note
from plzkeepthat.models.util.dbutils import get_notes_by_tag
import plzkeepthat.views.util.webutils as utils
import logging

#===============================================================================
# View classes that are rendered as response
#===============================================================================

class Edit(object):
    def __init__(self, form, id, note):
        self.form = form

        self.id = id
        self.note = note
        self.template = 'edit'

    def render(self):

        return utils.respond(self.template,
               {
                "note": self.note,
                "form": self.form,
                "target": "/edit/" + self.id
               })


class Details(object):
    def __init__(self, note, tags):
        self.note = note
        self.tags = tags
        self.template = 'details'

    def render(self):
        return utils.respond(self.template,
               {
                "note": self.note,
                "notetags": self.tags
               })


class Share(object):
    def __init__(self, form, note):
        self.note = note
        self.form = form
        self.mail_helptext = messages.MAIL_HELPTEXT

    def render(self):
        return utils.respond('share',
                       {
                        "note": self.note,
                        "form":  self.form
                        })

class Home(object):
    def __init__(self, form, page = 0):
        self.form = form
        self.page = page
        self.notes = util.get_notelist(users.get_current_user())
        self.template = 'home'
        self.helptexts = {'tags' : messages.TAGS_HELPTEXT }

    def render(self):
        results = Query(Note).filter('noteList = ', self.notes).order('-created').fetch(app.LIMIT)
        paginator = ObjectPaginator(results, app.PAGE)
        try: notes = paginator.get_page(self.page)
        except: notes = paginator.get_page(paginator.pages - 1)
        
        next = None
        prev = None
        logging.info(self.page)
        logging.info(paginator.has_previous_page(self.page))
        if paginator.has_previous_page(self.page): prev = self.page - 1
        if paginator.has_next_page(self.page): next = self.page + 1
        
        return utils.respond(self.template,
                       {
                       "notes": notes,
                       "form": self.form,
                       "next": next,
                       "has_prev": prev != None,
                       "prev": prev,
                       "target": "/",
                       "helptext": self.helptexts
                       })

class About(object):
    def __init__(self):
        pass

    def render(self):
        return utils.respond('about', {})

class Options(object):
    def __init__(self):
        pass

    def render(self):
        return utils.respond('options')

class TagFilter(object):
    def __init__(self, tag, page = 1):
        self.page = page
        self.tag = tag
        self.template = 'home'
        self.__filter_prefix = "/".join(["/filter", self.tag.text])
        self.notes = util.get_notelist(users.get_current_user())

    def render(self):
        results =  get_notes_by_tag(self.tag, self.notes, app.LIMIT)
        paginator = ObjectPaginator(results, app.PAGE)
        try: notes = paginator.get_page(self.page)
        except: notes = paginator.get_page(paginator.pages - 1)     
        
        next = None
        prev = None
        
        if paginator.has_previous_page(self.page): prev = "/".join(( self.__filter_prefix, str(self.page - 1)))
        if paginator.has_next_page(self.page): next = "/".join(( self.__filter_prefix, str(self.page + 1)))
        
        return utils.respond(self.template,
                       {
                       "notes": notes,
                       "next": next,
                       "has_prev": prev != None,
                       "prev": prev,
                       "target": "/",
                       })

class Redirect(object):
    def __init__(self, target):
        if target is "home": target = "/"
        if not target.startswith("/"): target = "/".join(target)
        self.target = target

    def render(self):
        return HttpResponseRedirect(self.target)

class RawResponse(object):
    def __init__(self, data, mimetype):
        self.data = data
        self.mimetype = mimetype
    def render(self):
        return utils.raw_response(self.data, self.mimetype)
